home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / Scripting the Finder / Zawphing / ReportError.cp < prev    next >
Encoding:
Text File  |  1994-10-11  |  5.8 KB  |  236 lines  |  [TEXT/MMCC]

  1. /*================================================================================
  2.     reportError.c
  3.     
  4.     ©1991 Greg Anderson
  5.     greggor@apple.com
  6.         
  7.     These routines are very convenient for debugging; they will convert
  8.     OSErrors from short negative numbers to human-readable strings in
  9.     Pascal format
  10.     
  11.     This code depends on:
  12.     
  13.         stringUtilities.c
  14.         dialogUtilities.c
  15.         
  16.         'DLOG' resource ID 30303    A simple dialog box for displaying errors;
  17.         'DITL' resource ID 30303    Has one 'Okay' button and one static text
  18.                                     item that reads:
  19.                                     
  20.                                     ^0 ^1
  21.                                     
  22.                                     ^2 because ^3
  23.                                     
  24.         'TEXT' resource ID 30303    All error messages, each line cr-terminated
  25.         
  26. ================================================================================*/
  27. #include "ReportError.h"
  28.  
  29. /*
  30. // Header files for utility routines:
  31. */
  32. #ifndef __DIALOGUTILITIES__
  33.     #include "DialogUtilities.h"
  34. #endif
  35. #ifndef __STRINGUTILITIES__
  36.     #include "StringUtilities.h"
  37. #endif
  38.  
  39. /*
  40. // Macintosh includes
  41. */
  42. #ifndef __TYPES__
  43.     #include <Types.h>
  44. #endif
  45. #ifndef __ERRORS__
  46.     #include <Errors.h>
  47. #endif
  48. #ifndef __MEMORY__
  49.     #include <Memory.h>
  50. #endif
  51. #ifndef __RESOURCES__
  52.     #include <Resources.h>
  53. #endif
  54.  
  55. /*
  56. // MPW requires this, but Think C 4.0.5 won't compile if
  57. // it's in (but works all the same without it).  The
  58. // error is 'Debug Table Overflow', & you can get it
  59. // just by including <Script.h> (which <Packages.h>
  60. // includes).  It looks like a size problem, because
  61. // other files can include <Script.h> with no problem.
  62. //
  63. // MPW needs <Packages.h> for 'NumToString'.
  64. */
  65. #ifndef THINK_C
  66.     #ifndef __PACKAGES__
  67.         #include <Packages.h>
  68.     #endif
  69. #endif
  70.  
  71. Handle        gResultCodeText = nil;
  72.  
  73. /*----------------------------------------------------------------------
  74.     Return 'true' if the 'sc6' button should be shown
  75. ----------------------------------------------------------------------*/
  76. Boolean StackChainRecordingInstalled()
  77. {
  78.     return false;
  79. }
  80.  
  81. /*----------------------------------------------------------------------
  82.     Call the stack trace dialog, if installed
  83. ----------------------------------------------------------------------*/
  84. void CallInstalledShowStackProc()
  85. {
  86.     //extern StackChainInfo    gStackChainInfo;
  87.  
  88.     //ShowStackChain( "\pStack chain at time of failure (tracing A6 links):", &gStackChainInfo );
  89. }
  90.  
  91. /*----------------------------------------------------------------------
  92.     Search for an error result code in the result code text
  93.     
  94.     Return 'false' if the name and description strings cannot be filled in
  95. ----------------------------------------------------------------------*/
  96. Boolean FindResultCodeDescription( OSErr theErr, Str255 errorNameStr, Str255 errorDescStr )
  97. {
  98.     Size        textSize;
  99.     char*        p;
  100.     Boolean        foundEntry;
  101.     OSErr        testError;
  102.     
  103.     /*
  104.     // Note that if the error is a 'memFullErr', it isn't going to do
  105.     // much good to try to load in the result code test.  Handle this
  106.     // case separately.
  107.     //
  108.     // While there are other situations that could also cause this
  109.     // routine to fail, none are as common as memFullErr.
  110.     */
  111.     if( theErr == memFullErr )
  112.     {
  113.         CtoPcpy( errorNameStr, "memFullErr" );
  114.         CtoPcpy( errorDescStr, "there is insufficient memory left in the heap zone" );
  115.         
  116.         return true;
  117.     }
  118.     /*
  119.     // If we have never loaded the result code text, do so now
  120.     */
  121.     if( gResultCodeText == nil )
  122.         gResultCodeText = GetResource( 'TEXT', kReportErrorID );
  123.     if( gResultCodeText == nil )
  124.         return false;
  125.     /*
  126.     // If the result code text has been purged, try to re-load it.
  127.     */
  128.     if( *gResultCodeText == nil )
  129.         LoadResource( gResultCodeText );
  130.     if( *gResultCodeText == nil )
  131.         return false;
  132.     /*
  133.     // Lock the result text & dereference it
  134.     */
  135.     MoveHHi( gResultCodeText );
  136.     HLock( gResultCodeText );
  137.     p = *gResultCodeText;
  138.     textSize = GetHandleSize( gResultCodeText );
  139.     foundEntry = false;
  140.     do
  141.     {
  142.         SkipToSpec( p, '-' );
  143.         if( !(*p) )
  144.             break;
  145.         ++p;
  146.         
  147.         testError = -ScanNumberInString( &p );
  148.         if( testError == theErr )
  149.         {
  150.             SkipPastWhitespace( p );
  151.             ScanWordInString( &p, errorNameStr );
  152.             SkipPastWhitespace( p );
  153.             ScanLineInString( &p, errorDescStr );
  154.             foundEntry = true;
  155.             break;
  156.         }
  157.     }
  158.     while( testError != -32767 );
  159.     /*
  160.     // Unlock the result text and return
  161.     */
  162.     HUnlock( gResultCodeText );
  163.     return foundEntry;
  164. }
  165.  
  166. /*----------------------------------------------------------------------
  167.     Put up a dialog box reporting an error to the user
  168. ----------------------------------------------------------------------*/
  169. void ReportError( Str255 errorMsg, OSErr theErr )
  170. {
  171.     DialogPtr        dlog;
  172.     short            itemHit;
  173.     Str255            errorNumberStr;
  174.     Str255            errorNameStr;
  175.     Str255            errorDescStr;
  176.     long            longErr = theErr;
  177.     
  178.     /*
  179.     // Do nothing if theErr == noErr
  180.     */
  181.     if( theErr != noErr )
  182.     {
  183.         /*
  184.         // Clear out the pascal strings, just to be safe
  185.         */
  186.         errorNameStr[0] = 0;
  187.         errorDescStr[0] = 0;
  188.         errorNumberStr[0] = 0;
  189.         /*
  190.         // Set up the error number string
  191.         */
  192.         NumToString( longErr, errorNumberStr );
  193.         FindResultCodeDescription( theErr, errorNameStr, errorDescStr );
  194.         if( errorDescStr[0] == 0 )
  195.             CtoPcpy( errorDescStr, "an error ocurred" );
  196.         /*
  197.         // Set up Dialog box
  198.         */
  199.         dlog = GetNewDialog(kReportErrorID, (Ptr)0L, (WindowPtr)-1L);
  200.         if( dlog != nil )
  201.         {
  202.             SetPort( dlog );
  203.             ParamText( errorMsg, errorNumberStr, errorNameStr, errorDescStr );
  204.             SelectWindow(dlog);
  205.             InstallDefaultOutline( dlog, 1 );
  206.             if(StackChainRecordingInstalled() == false)
  207.                 HideDItem(dlog, 2);
  208.             CenterAndShowDialog(dlog);
  209.             InitCursor();
  210.             /*
  211.             // Wait for user to click "Okay" or "sc6"
  212.             */
  213.             do
  214.             {
  215. #if USESROUTINEDESCRIPTORS
  216.                 ModalDialog((ModalFilterUPP) &gCutPasteFilterRD, &itemHit);
  217. #else
  218.                 ModalDialog((ModalFilterUPP) CutPasteFilter, &itemHit);
  219. #endif
  220.             } while( (itemHit != 1) && (itemHit != 2) );
  221.             DisposDialog(dlog);
  222.             /*
  223.             // If user hit 'sc6', then show the stack chain
  224.             */
  225.             if( itemHit == 2 )
  226.             {
  227.                 CallInstalledShowStackProc();
  228.             }
  229.         }
  230.         else
  231.         {
  232.             DebugStr( "\pReportError could not bring up dialog box" );
  233.         }
  234.     }
  235. }
  236.